/**
 * @author mapeng
 * @version 2017-05-09
 */

(function(doc, win) {
  var pageVariables = {
    shopId: "", //门店Id
    type: "", //操作类型：新增/编辑
    handleForward: "load",
    handleBack: "back",
    weixinAccount: {} //开通失败编辑
  };

  $(doc).on("pageInit", "#weixinOpenStep1Page4Wangshang", function(
    e,
    pageId,
    $page,
    isBack,
    isForward
  ) {
    if (isForward) return;

    if (isBack) {
      handlePageBack();
      return;
    }

    init4Page();
  });
  bindEvents4Page();

  //页面返回
  function handlePageBack() {
    var params = CommonUtil.takeOutParamObject() || {};
    pageVariables.handleForward = params.handleForward || "load";

    params.handleForward = "";
    CommonUtil.injectParamObject(params);
  }

  //前进
  function doForward() {
    if (pageVariables.handleForward == "forward") {
      $.router.forward();
    } else if (pageVariables.handleForward == "load") {
      //编辑
      var cache = CommonUtil.takeOutParamObject() || {};
      cache.weixinAccount = pageVariables.weixinAccount;
      CommonUtil.injectParamObject(cache);
      $.router.load("weixin_open_step2_wangshang.html", true); // ignore cache
    }
  }

  //页面进入
  function handlePageLoad() {
    var params = CommonUtil.takeOutParamObject() || {};
    pageVariables.handleBack = params.handleBack || "back";
    params.handleBack = "";
    CommonUtil.injectParamObject(params);
    pageVariables.handleForward = "load";
  }

  //返回
  function doBack() {
    //返回门店列表
    location.href = '../vuepages/index.html#/storeManage?'+App.urlParam;
  }

  //页面初始化
  function init4Page() {
    //参数
    var params = CommonUtil.takeOutParamObject() || {};
    pageVariables.shopId = params.shopId;
    pageVariables.type = params.type;
    if (!params.type) {
      //参数错误
      params.type = "1";
      alert("pageVariables.type is null 开发测试临时增加");
      CommonUtil.injectParamObject(params);
      init4Page();
      return;
    }

    handlePageLoad();

    //初始化商户类型
    init4MerchantType();

    //初始化经营类目
    init4BusinessRange();
    //编辑标识
    var editFlag = pageVariables.type == Constants.weixin.OPERATE_TYPE.edit;
    edit4Page(editFlag);
  }

  //初始化页面值：编辑
  function edit4Page(editFlag) {
    //查询微信账户
    CommonUtil.sendJsonpRequest({
      // apiTest.testGetAccount({
      url: App.serverUrl + "/app/wxAccountCloudBox/getLastShopAccountDetail.do",
      params: { shopId: pageVariables.shopId },
      success: function(result) {
        if (result.success && result.success.toString() == "true") {
          editFlag = (result.resultObject.shopId && result.resultObject.shopId==pageVariables.shopId);
          if (editFlag) {
            //是否审核中
            if(result.resultObject.status=='3'){
              DialogUtil.dialogConfirm({
                title: "提示",
                content: "进件审批中，暂不支持操作。",
                btnLeft:"返回",
                btnRight: "确定",
                callbackLeft:doBack,
                callbackRight: doBack
              });
            }else{
              bindPageData(result);
            }
          } else {
            if (result.resultObject && result.resultObject.shopId) {
              DialogUtil.dialogConfirm({
                title: "提示",
                content: "是否使用上次已提交的进件资料？",
                btnLeft: "取消",
                btnRight: "确定",
                callbackRight: function() {
                  bindPageData(result);
                }
              });
            } else {
              //第一次设置，绑定数据。主要字段：邀请码
              bindPageData(result);
            }
          }
        } else {
          //business error
          if (editFlag) {
            DialogUtil.dialogWarn(result.msg);
          }
        }
      },
      error: function() {
        //not 200
        DialogUtil.dialogWarn("请求失败，请稍候重试");
      }
    });
  }

  /**
   * 绑定页面数据
   */
  function bindPageData(result) {
    var weixinAccount = result.resultObject;
    pageVariables.weixinAccount = weixinAccount;

    $("#weixinOpenStep1Page4Wangshang input[name='shopName']").val(
      weixinAccount.merchantName
    ); //门店名称
    $("#weixinOpenStep1Page4Wangshang input[name='shortShopName']").val(
      weixinAccount.merchantShortName
    ); //简称

    //商户类型
    var merchantType = weixinAccount.merchantType;
    if (merchantType) {
      $("#weixinOpenStep1Page4Wangshang div[name='merchantType']")
        .children("ul")
        .mobiscroll(
          "setVal",
          Constants.weixin_wangshang.getMerchantTypeText(merchantType),
          true
        );
      $(
        "#weixinOpenStep1Page4Wangshang div[name='merchantType'] input[type='hidden']"
      ).val(merchantType);
      setFields4BusinessType(merchantType);
    }

    //经营类目
    var businessRange = weixinAccount.mcc;
    $("#weixinOpenStep1Page4Wangshang div[name='businessRange']")
      .children("ul")
      .mobiscroll(
        "setVal",
        Constants.weixin_wangshang.getBusinessRangeText(businessRange),
        true
      );
    $(
      "#weixinOpenStep1Page4Wangshang div[name='businessRange'] input[type='hidden']"
    ).val(businessRange);

    $("#weixinOpenStep1Page4Wangshang input[name='hotline']").val(
      weixinAccount.servicePhoneNo
    ); //客服电话
    $("#weixinOpenStep1Page4Wangshang input[name='principalName']").val(
      weixinAccount.principal
    ); //负责人
    $("#weixinOpenStep1Page4Wangshang input[name='principalMobile']").val(
      weixinAccount.principalMobile
    ); //手机号
    $("#weixinOpenStep1Page4Wangshang input[name='principalCard']").val(
      weixinAccount.idCard
    );
    //身份证号

    //身份证正面
    var yzImgUrl4Front = weixinAccount.yzImageFrontUrl;
    var weixinImgUrl4Front = weixinAccount.identityCardPicFront;
    var yzImgUrl4Back = weixinAccount.yzImageReverseUrl;
    var weixinImgUrl4Back = weixinAccount.identityCardPicContrary;
    if (yzImgUrl4Front && weixinImgUrl4Front) {
      $("#weixinOpenStep1Page4Wangshang li[name='frontPicture'] img").attr(
        "src",
        commonActionYunzongUrl(yzImgUrl4Front)
      );

      $("#weixinOpenStep1Page4Wangshang li[name='frontPicture']").attr(
        "yzImgUrl",
        yzImgUrl4Front
      );

      $("#weixinOpenStep1Page4Wangshang li[name='frontPicture']").attr(
        "weixinImgUrl",
        weixinImgUrl4Front
      );

      displayPicture(
        $("#weixinOpenStep1Page4Wangshang li[name='frontPicture']"),
        "show"
      );
    }

    //身份证反面
    var yzImgUrl4Back = weixinAccount.yzImageReverseUrl;
    var weixinImgUrl4Back = weixinAccount.identityCardPicContrary;
    if (yzImgUrl4Back && weixinImgUrl4Back) {
      $("#weixinOpenStep1Page4Wangshang li[name='backPicture'] img").attr(
        "src",
        commonActionYunzongUrl(yzImgUrl4Back)
      );
      $("#weixinOpenStep1Page4Wangshang li[name='backPicture']").attr(
        "yzImgUrl",
        yzImgUrl4Back
      );

      $("#weixinOpenStep1Page4Wangshang li[name='backPicture']").attr(
        "weixinImgUrl",
        weixinImgUrl4Back
      );
      displayPicture(
        $("#weixinOpenStep1Page4Wangshang li[name='backPicture']"),
        "show"
      );
    }

    //工商注册号
    if (
      merchantType ==
        Constants.weixin_wangshang.MERCHANT_TYPE.person_business ||
      merchantType ==
        Constants.weixin_wangshang.MERCHANT_TYPE.enterprise_business
    ) {
      //个体工商户、企业商户
      $("#weixinOpenStep1Page4Wangshang input[name='registerNumber']").val(
        weixinAccount.bussAuthNum
      );
    }

    //营业执照
    if (
      merchantType ==
        Constants.weixin_wangshang.MERCHANT_TYPE.person_business ||
      merchantType ==
        Constants.weixin_wangshang.MERCHANT_TYPE.enterprise_business
    ) {
      //个体工商户、企业商户
      var yzImgUrl4BusinessLicense = weixinAccount.yzLicensePhoto;
      var weixinImgUrl4BusinessLicense = weixinAccount.licensePhoto;
      $("#weixinOpenStep1Page4Wangshang li[name='businessLicense'] img").attr(
        "src",
        commonActionYunzongUrl(yzImgUrl4BusinessLicense)
      );
      $("#weixinOpenStep1Page4Wangshang li[name='businessLicense']").attr(
        "yzImgUrl",
        yzImgUrl4BusinessLicense
      );
      $("#weixinOpenStep1Page4Wangshang li[name='businessLicense']").attr(
        "weixinImgUrl",
        weixinImgUrl4BusinessLicense
      );
      displayPicture(
        $("#weixinOpenStep1Page4Wangshang li[name='businessLicense']"),
        "show"
      );
    }

    //开户许可证
    if (
      merchantType ==
      Constants.weixin_wangshang.MERCHANT_TYPE.enterprise_business
    ) {
      //企业商户
      var yzImgUrl4AccountLicense = weixinAccount.yzIndustryLicensePhoto;
      var weixinImgUrl4AccountLicense = weixinAccount.industryLicensePhoto;
      $("#weixinOpenStep1Page4Wangshang li[name='accountLicense'] img").attr(
        "src",
        commonActionYunzongUrl(yzImgUrl4AccountLicense)
      );
      $("#weixinOpenStep1Page4Wangshang li[name='accountLicense']").attr(
        "yzImgUrl",
        yzImgUrl4AccountLicense
      );
      $("#weixinOpenStep1Page4Wangshang li[name='accountLicense']").attr(
        "weixinImgUrl",
        weixinImgUrl4AccountLicense
      );
      displayPicture(
        $("#weixinOpenStep1Page4Wangshang li[name='accountLicense']"),
        "show"
      );
    }

    listen4Next();
  }

  //初始化商户类型
  function init4MerchantType() {
    var merchantTypeUl = "<ul style='display:none;'>";
    $.each(Constants.weixin_wangshang.MERCHANT_TYPE, function(key, value) {
      merchantTypeUl +=
        "<li data-code='" +
        value +
        "' data-val='" +
        Constants.weixin_wangshang.getMerchantTypeText(value) +
        "'>" +
        Constants.weixin_wangshang.getMerchantTypeText(value) +
        "</li>";
    });
    merchantTypeUl += "</ul>";
    $("#weixinOpenStep1Page4Wangshang div[name='merchantType']").prepend(
      merchantTypeUl
    );

    $("#weixinOpenStep1Page4Wangshang div[name='merchantType']")
      .children("ul")
      .mobiscroll()
      .treelist({
        theme: "ios",
        display: "bottom",
        lang: "zh",
        placeholder: "请选择商户类型",
        onSelect: function(valueText, inst) {
          var $ul = $(this);
          var $li = $ul.children("li[data-val='" + valueText + "']");
          var code = $li.attr("data-code");
          $ul
            .parent()
            .find("input[type='hidden']")
            .val(code);

          setFields4BusinessType(code);

          listen4Next();
        }
      });

    //默认“企业商户”
    $("#weixinOpenStep1Page4Wangshang div[name='merchantType']")
      .children("ul")
      .mobiscroll(
        "setVal",
        Constants.weixin_wangshang.getMerchantTypeText(
          Constants.weixin_wangshang.MERCHANT_TYPE.enterprise_business
        ),
        true
      );
    $(
      "#weixinOpenStep1Page4Wangshang div[name='merchantType'] input[type='hidden']"
    ).val(Constants.weixin_wangshang.MERCHANT_TYPE.enterprise_business);
    setFields4BusinessType(
      Constants.weixin_wangshang.MERCHANT_TYPE.enterprise_business
    );
  }

  //初始化经营类目
  function init4BusinessRange() {
    var businessRangeTypeUl = "<ul style='display:none;'>";
    $.each(Constants.weixin_wangshang.BUSINESS_RANGE, function(key, value) {
      businessRangeTypeUl +=
        "<li data-code='" +
        value +
        "' data-val='" +
        Constants.weixin_wangshang.getBusinessRangeText(value) +
        "'>" +
        Constants.weixin_wangshang.getBusinessRangeText(value) +
        "</li>";
    });
    businessRangeTypeUl += "</ul>";
    $("#weixinOpenStep1Page4Wangshang div[name='businessRange']").prepend(
      businessRangeTypeUl
    );

    $("#weixinOpenStep1Page4Wangshang div[name='businessRange']")
      .children("ul")
      .mobiscroll()
      .treelist({
        theme: "ios",
        display: "bottom",
        lang: "zh",
        placeholder: "请选择经营类目",
        onSelect: function(valueText, inst) {
          var $ul = $(this);
          var $li = $ul.children("li[data-val='" + valueText + "']");
          var code = $li.attr("data-code");
          $ul
            .parent()
            .find("input[type='hidden']")
            .val(code);

          listen4Next();
        }
      });
  }

  //可见字段：商户类型
  //企业法人：仅企业商户显示
  //自然人：三项都不许要填；
  //个体工商户：工商注册号、营业执照；
  //企业商户：工商注册号、营业执照、开户许可证
  function setFields4BusinessType(businessType) {
    if (
      businessType == Constants.weixin_wangshang.MERCHANT_TYPE.nature_person
    ) {
      //自然人
      // $("#weixinOpenStep1Page4Wangshang input[name='bossName']").parents("li").css("display","none");//企业法人
      $("#weixinOpenStep1Page4Wangshang div[name='licenseTitle']").css(
        "display",
        "none"
      ); //营业执照标题
      $("#weixinOpenStep1Page4Wangshang input[name='registerNumber']")
        .parents("li")
        .css("display", "none"); //工商注册号
      $("#weixinOpenStep1Page4Wangshang li[name='businessLicense']")
        .parents("div.license")
        .css("display", "none"); //营业执照
      $("#weixinOpenStep1Page4Wangshang li[name='accountLicense']")
        .parents("div.license")
        .css("display", "none"); //开户许可证
    } else if (
      businessType == Constants.weixin_wangshang.MERCHANT_TYPE.person_business
    ) {
      //个体工商户
      // $("#weixinOpenStep1Page4Wangshang input[name='bossName']").parents("li").css("display","none");
      $("#weixinOpenStep1Page4Wangshang div[name='licenseTitle']").css(
        "display",
        ""
      );
      $("#weixinOpenStep1Page4Wangshang input[name='registerNumber']")
        .parents("li")
        .css("display", "");
      $("#weixinOpenStep1Page4Wangshang li[name='businessLicense']")
        .parents("div.license")
        .css("display", "");
      $("#weixinOpenStep1Page4Wangshang li[name='accountLicense']")
        .parents("div.license")
        .css("display", "none");
    } else if (
      businessType ==
      Constants.weixin_wangshang.MERCHANT_TYPE.enterprise_business
    ) {
      //企业商户
      // $("#weixinOpenStep1Page4Wangshang input[name='bossName']").parents("li").css("display","");
      $("#weixinOpenStep1Page4Wangshang div[name='licenseTitle']").css(
        "display",
        ""
      );
      $("#weixinOpenStep1Page4Wangshang input[name='registerNumber']")
        .parents("li")
        .css("display", "");
      $("#weixinOpenStep1Page4Wangshang li[name='businessLicense']")
        .parents("div.license")
        .css("display", "");
      $("#weixinOpenStep1Page4Wangshang li[name='accountLicense']")
        .parents("div.license")
        .css("display", "");
    }
  }

  //校验返回
  function confirm4Back() {
    var shopName = $("#weixinOpenStep1Page4Wangshang input[name='shopName']")
      .val()
      .trim(); //门店名称
    var shortShopName = $(
      "#weixinOpenStep1Page4Wangshang input[name='shortShopName']"
    )
      .val()
      .trim(); //简称
    var merchantType = $(
      "#weixinOpenStep1Page4Wangshang div[name='merchantType'] input[type='hidden']"
    ).val(); //商户类型
    var businessRange = $(
      "#weixinOpenStep1Page4Wangshang div[name='businessRange'] input[type='hidden']"
    ).val(); //经营类目
    var hotline = $("#weixinOpenStep1Page4Wangshang input[name='hotline']")
      .val()
      .trim(); //客服电话
    var principalName = $(
      "#weixinOpenStep1Page4Wangshang input[name='principalName']"
    )
      .val()
      .trim(); //负责人
    var principalMobile = $(
      "#weixinOpenStep1Page4Wangshang input[name='principalMobile']"
    )
      .val()
      .trim(); //手机号
    var principalCard = $(
      "#weixinOpenStep1Page4Wangshang input[name='principalCard']"
    )
      .val()
      .trim(); //身份证号码
    var yzImgUrl4Front = $(
      "#weixinOpenStep1Page4Wangshang li[name='frontPicture']"
    ).attr("yzImgUrl"); //身份证正面：云纵
    var weixinImgUrl4Front = $(
      "#weixinOpenStep1Page4Wangshang li[name='frontPicture']"
    ).attr("weixinImgUrl"); //身份证正面：微信
    var yzImgUrl4Back = $(
      "#weixinOpenStep1Page4Wangshang li[name='backPicture']"
    ).attr("yzImgUrl"); //身份证反面：云纵
    var weixinImgUrl4Back = $(
      "#weixinOpenStep1Page4Wangshang li[name='backPicture']"
    ).attr("weixinImgUrl"); //身份证反面：微信

    //没有填写任何表单
    if (
      !shopName &&
      !shortShopName &&
      merchantType ==
        Constants.weixin_wangshang.MERCHANT_TYPE.enterprise_business &&
      !businessRange &&
      !hotline &&
      !principalName &&
      !principalMobile &&
      !principalCard &&
      !yzImgUrl4Front &&
      !yzImgUrl4Back
    ) {
      if (
        merchantType == Constants.weixin_wangshang.MERCHANT_TYPE.nature_person
      ) {
        //自然人
        doBack();
        return;
      } else if (
        merchantType == Constants.weixin_wangshang.MERCHANT_TYPE.person_business
      ) {
        //个体工商户
        var registerNumber = $(
          "#weixinOpenStep1Page4Wangshang input[name='registerNumber']"
        )
          .val()
          .trim(); //工商注册号
        var yzImgUrl4BusinessLicense = $(
          "#weixinOpenStep1Page4Wangshang li[name='businessLicense']"
        ).attr("yzImgUrl"); //营业执照：云纵
        if (!registerNumber && !yzImgUrl4BusinessLicense) {
          doBack();
          return;
        }
      } else if (
        merchantType ==
        Constants.weixin_wangshang.MERCHANT_TYPE.enterprise_business
      ) {
        //企业商户
        // var bossName = $("#weixinOpenStep1Page4Wangshang input[name='bossName']").val().trim(); //企业法人
        var registerNumber = $(
          "#weixinOpenStep1Page4Wangshang input[name='registerNumber']"
        )
          .val()
          .trim(); //工商注册号
        var yzImgUrl4BusinessLicense = $(
          "#weixinOpenStep1Page4Wangshang li[name='businessLicense']"
        ).attr("yzImgUrl"); //营业执照：云纵
        var yzImgUrl4AccountLicense = $(
          "#weixinOpenStep1Page4Wangshang li[name='accountLicense']"
        ).attr("yzImgUrl"); //开户人许可证：云纵
        if (
          !registerNumber &&
          !yzImgUrl4BusinessLicense &&
          !yzImgUrl4AccountLicense
        ) {
          doBack();
          return;
        }
      }
    }

    //已填写：需要确认是否放弃
    DialogUtil.dialogConfirm({
      title: "",
      content: "离开页面将不会保存本次修改，确定要离开吗？",
      btnLeft: "取消",
      btnRight: "确定",
      callbackLeft: function() {},
      callbackRight: function() {
        doBack();
      }
    });
  }

  //页面事件绑定
  function bindEvents4Page() {
    //返回
    $(document).on(
      "click",
      "#weixinOpenStep1Page4Wangshang .header a.back",
      function(e) {
        confirm4Back();
      }
    );

    //监听输入
    var listenFormStr =
      "#weixinOpenStep1Page4Wangshang input[name='shopName']," +
      "#weixinOpenStep1Page4Wangshang input[name='shortShopName']," +
      "#weixinOpenStep1Page4Wangshang input[name='hotline']," +
      // "#weixinOpenStep1Page4Wangshang input[name='bossName']," +
      "#weixinOpenStep1Page4Wangshang input[name='principalName']," +
      "#weixinOpenStep1Page4Wangshang input[name='principalMobile']," +
      "#weixinOpenStep1Page4Wangshang input[name='principalCard']," +
      "#weixinOpenStep1Page4Wangshang input[name='registerNumber']";
    $(document).on("input", listenFormStr, function(e) {
      listen4Next();
    });
    $(document).on(
      "clearInput",
      "#weixinOpenStep1Page4Wangshang .zhk-list_gb",
      function(e) {
        listen4Next();
      }
    );

    //证件照片上传：身份证正面、身份证反面、营业执照、开户许可证
    $(document).on(
      "click",
      "#weixinOpenStep1Page4Wangshang li.picture",
      function(e) {
        if ($(e.target).hasClass("zhk-list_gb")) return;

        uploadPicture($(this));
      }
    );
    $(document).on(
      "click",
      "#weixinOpenStep1Page4Wangshang li.picture .zhk-list_gb",
      function(e) {
        var $li = $(this).parents("li.picture");
        displayPicture($li, "hide");

        $li.attr("yzImgUrl", "");
        $li.attr("weixinImgUrl", "");

        listen4Next();
      }
    );

    //下一步
    $(document).on(
      "click",
      "#weixinOpenStep1Page4Wangshang .header a[name='nextStep']",
      function(e) {
        if (
          $(this)
            .parents("li.right")
            .hasClass("disabled")
        )
          return;

        next();
      }
    );
  }

  //下一步
  function next() {
    //表单数据
    var shopName = $("#weixinOpenStep1Page4Wangshang input[name='shopName']")
      .val()
      .trim(); //门店名称
    var shortShopName = $(
      "#weixinOpenStep1Page4Wangshang input[name='shortShopName']"
    )
      .val()
      .trim(); //简称
    var merchantType = $(
      "#weixinOpenStep1Page4Wangshang div[name='merchantType'] input[type='hidden']"
    ).val(); //商户类型
    var businessRange = $(
      "#weixinOpenStep1Page4Wangshang div[name='businessRange'] input[type='hidden']"
    ).val(); //经营类目
    var hotline = $("#weixinOpenStep1Page4Wangshang input[name='hotline']")
      .val()
      .trim(); //客服电话
    // var bossName = "";
    var principalName = $(
      "#weixinOpenStep1Page4Wangshang input[name='principalName']"
    )
      .val()
      .trim(); //负责人
    var principalMobile = $(
      "#weixinOpenStep1Page4Wangshang input[name='principalMobile']"
    )
      .val()
      .trim(); //手机号
    var principalCard = $(
      "#weixinOpenStep1Page4Wangshang input[name='principalCard']"
    )
      .val()
      .trim(); //身份证号码
    var yzImgUrl4Front = $(
      "#weixinOpenStep1Page4Wangshang li[name='frontPicture']"
    ).attr("yzImgUrl"); //身份证正面：云纵
    var weixinImgUrl4Front = $(
      "#weixinOpenStep1Page4Wangshang li[name='frontPicture']"
    ).attr("weixinImgUrl"); //身份证正面：微信
    var yzImgUrl4Back = $(
      "#weixinOpenStep1Page4Wangshang li[name='backPicture']"
    ).attr("yzImgUrl"); //身份证反面：云纵
    var weixinImgUrl4Back = $(
      "#weixinOpenStep1Page4Wangshang li[name='backPicture']"
    ).attr("weixinImgUrl"); //身份证反面：微信
    var registerNumber = ""; //工商注册号
    var yzImgUrl4BusinessLicense = ""; //营业执照：云纵
    var weixinImgUrl4BusinessLicense = ""; //营业执照：微信
    var yzImgUrl4AccountLicense; //开户许可证照片：云纵
    var weixinImgUrl4AccountLicense; //开户许可证照片：微信

    //校验：门店名称
    if (shopName.length == 0) {
      DialogUtil.dialogWarn("请填写门店名称");
      return;
    }
    if (
      !/^[\dA-Za-z\u4e00-\u9fa5\u0000-\u00ff\uff00-\uffff\\(\\)（）]+$/.test(
        shopName
      )
    ) {
      DialogUtil.dialogWarn("门店名称不允许出现特殊字符");
      return;
    }
    if (shopName.length > 64) {
      DialogUtil.dialogWarn("门店名称长度不能超过64");
      return;
    }

    //校验：简称
    if (shortShopName.length == 0) {
      DialogUtil.dialogWarn("请填写简称");
      return;
    }
    // if(!/^[\dA-Za-z\u4e00-\u9fa5]+$/.test(shortShopName)){
    // 	DialogUtil.dialogWarn("简称仅允许输入中文、英文、数字");
    // 	return;
    // }
    if (shortShopName.length > 64) {
      DialogUtil.dialogWarn("简称长度不能超过64");
      return;
    }

    //校验：商户类型
    if (!merchantType) {
      DialogUtil.dialogWarn("请选择商户类型");
      return;
    }

    //校验：经营类目
    if (!businessRange) {
      DialogUtil.dialogWarn("请选择经营类目");
      return;
    }

    //校验：客服电话
    if (hotline.length == 0) {
      DialogUtil.dialogWarn("请填写客服电话");
      return;
    }
    if (!/^\d{1,20}$/.test(hotline)) {
      DialogUtil.dialogWarn("请填写有效的客服电话");
      return;
    }

    //校验：企业法人
    /*if(merchantType == Constants.weixin_wangshang.MERCHANT_TYPE.enterprise_business){
			bossName = $("#weixinOpenStep1Page4Wangshang input[name='bossName']").val().trim();
			if(bossName.length == 0){
				DialogUtil.dialogWarn("请填写企业法人");
				return;
			}
			if(!/^[\dA-Za-z\u4e00-\u9fa5]+$/.test(bossName)){
				DialogUtil.dialogWarn("企业法人仅允许输入中文、英文、数字");
				return;
			}
			if(bossName.length > 64){
				DialogUtil.dialogWarn("企业法人长度不能超过64");
				return;
			}
		}*/

    //校验：负责人
    if (principalName.length == 0) {
      DialogUtil.dialogWarn("请填写负责人姓名");
      return;
    }
    if (!/^[\dA-Za-z\u4e00-\u9fa5\\(\\)（）]+$/.test(principalName)) {
      DialogUtil.dialogWarn("负责人姓名不允许出现特殊字符");
      return;
    }
    if (principalName.length > 64) {
      DialogUtil.dialogWarn("负责人长度不能超过64");
      return;
    }

    //校验：手机号
    if (principalMobile.length == 0) {
      DialogUtil.dialogWarn("请填写负责人手机号");
      return;
    }
    if (!/^[1][3578][0-9]{9}$/.test(principalMobile)) {
      DialogUtil.dialogWarn("请填写有效的手机号");
      return;
    }

    //校验：身份证号码
    if (principalCard.length == 0) {
      DialogUtil.dialogWarn("请填写负责人身份证号码");
      return;
    }
    if (!/(^\d{15}$)|(^\d{17}([0-9]|X)$)/.test(principalCard)) {
      DialogUtil.dialogWarn("请填写有效的身份证号码");
      return;
    }

    //校验：身份证照片
    if (!(yzImgUrl4Front && yzImgUrl4Back)) {
      DialogUtil.dialogWarn("请上传身份证照片");
      return;
    }
    if (!yzImgUrl4Front) {
      DialogUtil.dialogWarn("请上传正面身份证");
      return;
    }
    if (!yzImgUrl4Back) {
      DialogUtil.dialogWarn("请上传反面身份证");
      return;
    }

    //校验：工商注册号
    if (
      merchantType ==
        Constants.weixin_wangshang.MERCHANT_TYPE.person_business ||
      merchantType ==
        Constants.weixin_wangshang.MERCHANT_TYPE.enterprise_business
    ) {
      registerNumber = $(
        "#weixinOpenStep1Page4Wangshang input[name='registerNumber']"
      )
        .val()
        .trim(); //工商注册号
      if (registerNumber.length == 0) {
        DialogUtil.dialogWarn("请填写工商注册号");
        return;
      }
      if (!/(^[\dA-Za-z]{15}$)|(^[\dA-Za-z]{18}$)/.test(registerNumber)) {
        DialogUtil.dialogWarn("请填写有效的工商注册号");
        return;
      }
    }

    //校验：营业执照
    if (
      merchantType ==
        Constants.weixin_wangshang.MERCHANT_TYPE.person_business ||
      merchantType ==
        Constants.weixin_wangshang.MERCHANT_TYPE.enterprise_business
    ) {
      yzImgUrl4BusinessLicense = $(
        "#weixinOpenStep1Page4Wangshang li[name='businessLicense']"
      ).attr("yzImgUrl"); //营业执照：云纵
      weixinImgUrl4BusinessLicense = $(
        "#weixinOpenStep1Page4Wangshang li[name='businessLicense']"
      ).attr("weixinImgUrl"); //营业执照：微信
      if (!yzImgUrl4BusinessLicense) {
        DialogUtil.dialogWarn("请上传营业执照照片");
        return;
      }
    }

    //校验：开户许可证照片
    if (
      merchantType ==
      Constants.weixin_wangshang.MERCHANT_TYPE.enterprise_business
    ) {
      yzImgUrl4AccountLicense = $(
        "#weixinOpenStep1Page4Wangshang li[name='accountLicense']"
      ).attr("yzImgUrl"); //开户人许可证：云纵
      weixinImgUrl4AccountLicense = $(
        "#weixinOpenStep1Page4Wangshang li[name='accountLicense']"
      ).attr("weixinImgUrl"); //开户人许可证：微信
      if (!yzImgUrl4AccountLicense) {
        DialogUtil.dialogWarn("请上传开户人许可证照片");
        return;
      }
    }
    // 校验手机号是否在黑名单内
    CommonUtil.sendJsonpRequest({
  	url: App.serverUrl+"/app/wxAccount/verifyMobile.do",
    async:false,
	  params : {"mobile":principalMobile},
		success : function(result){
			if(result.code == -1){//校验不通过
		  	DialogUtil.dialogWarn(result.msg);
        return;
			}else{
    //保存本页数据
    var cache = CommonUtil.takeOutParamObject() || {};
    cache.stepOne4Wangshang = {
      shopId: pageVariables.shopId,
      type: pageVariables.type,
      merchantName: shopName, //门店名称
      merchantShortName: shortShopName, //简称
      merchantType: merchantType, //商户类型
      mcc: businessRange, //经营类目
      mccName: Constants.weixin_wangshang.getBusinessRangeText(businessRange), //经营类目名称
      servicePhoneNo: hotline, //客服电话
      principal: principalName, //负责人
      principalMobile: principalMobile, //手机号
      idCard: principalCard, //身份证号码
      yzImageFrontUrl: yzImgUrl4Front, //身份证正面：云纵//修改为正面not yzImgUrl4Back
      identityCardPicFront: weixinImgUrl4Front, //身份证正面
      yzImageReverseUrl: yzImgUrl4Back, //身份证反面：云纵
      identityCardPicContrary: weixinImgUrl4Back //身份证反面
    };
    if (
      merchantType == Constants.weixin_wangshang.MERCHANT_TYPE.person_business
    ) {
      //个体工商户
      cache.stepOne4Wangshang.bussAuthNum = registerNumber; //工商注册号
      cache.stepOne4Wangshang.yzLicensePhoto = yzImgUrl4BusinessLicense; //营业执照：云纵
      cache.stepOne4Wangshang.licensePhoto = weixinImgUrl4BusinessLicense; //营业执照
    } else if (
      merchantType ==
      Constants.weixin_wangshang.MERCHANT_TYPE.enterprise_business
    ) {
      //企业商户
      // cache.stepOne4Wangshang.legalPeople = bossName;	//企业法人
      cache.stepOne4Wangshang.bussAuthNum = registerNumber; //工商注册号
      cache.stepOne4Wangshang.yzLicensePhoto = yzImgUrl4BusinessLicense; //营业执照：云纵
      cache.stepOne4Wangshang.licensePhoto = weixinImgUrl4BusinessLicense; //营业执照
      cache.stepOne4Wangshang.yzIndustryLicensePhoto = yzImgUrl4AccountLicense; //工商许可证：云纵
      cache.stepOne4Wangshang.industryLicensePhoto = weixinImgUrl4AccountLicense; //工商许可证
    }
    CommonUtil.injectParamObject(cache);

    //跳转
    doForward();
      }
		},
		error: function(){//not 200
			DialogUtil.dialogWarn("校验手机号失败,请检查网络稍后再尝试");
      return;
		},

   });

  }

  //图片上传：身份证正反面
  function uploadPicture(target) {
    var uploadType = target.attr("uploadType");

    $.actions([
      [
        {
          text: "相册",
          onClick: function() {
            //选择

            AppUtil.call(
              AppUtil.APIS.SELECT_PICTURE,
              {
                compress: { sizeLimit: "500", unit: "K" },
                format: "JPG",
                maxNum: "1"
              },
              function(selectResult) {
                if (selectResult.success.toString() == "true") {
                  var filePath = selectResult.resultObject[0];

                  //上传
                  common.loading("上传中");
                  AppUtil.call(
                    AppUtil.APIS.UPLOAD_PICTURE,
                    {
                      url: App.serverUrl + "/app/img/upload4Ant.do",
                      file: filePath,
                      data: { uploadType: uploadType }
                    },
                    function(uploadResult) {
                      common.closeLoading();
                      if (
                        uploadResult.success.toString() == "true" &&
                        uploadResult.resultObject.success.toString() == "true"
                      ) {
                        target.attr(
                          "yzImgUrl",
                          uploadResult.resultObject.resultObject.imageUrl
                        );
                        target.attr(
                          "weixinImgUrl",
                          uploadResult.resultObject.resultObject
                            .thirdPartyImageUrl
                        );
                        //显示图片
                        target.find("img").attr("src", filePath);
                        displayPicture(target, "show");

                        listen4Next();
                      } else {
                        var msg = "";
                        if (uploadResult.success.toString() == "false") {
                          msg = uploadResult.msg;
                        } else if (
                          uploadResult.resultObject.success.toString() ==
                          "false"
                        ) {
                          msg = uploadResult.resultObject.msg;
                        }
                        DialogUtil.dialogWarn(msg);
                      }
                    }
                  );
                } else {
                  DialogUtil.dialogWarn(selectResult.msg);
                }
              }
            );
          }
        },
        {
          text: "拍照",
          onClick: function() {
            //拍照
            AppUtil.call(
              AppUtil.APIS.TAKE_PICTURE,
              {
                compress: { sizeLimit: "500", unit: "K" },
                format: "JPG",
                maxNum: "1"
              },
              function(selectResult) {
                if (selectResult.success.toString() == "true") {
                  var filePath = selectResult.resultObject;

                  //上传
                  common.loading("上传中");
                  AppUtil.call(
                    AppUtil.APIS.UPLOAD_PICTURE,
                    {
                      url: App.serverUrl + "/app/img/upload4Ant.do",
                      file: filePath,
                      data: { uploadType: uploadType }
                    },
                    function(uploadResult) {
                      common.closeLoading();
                      if (
                        uploadResult.success.toString() == "true" &&
                        uploadResult.resultObject.success.toString() == "true"
                      ) {
                        target.attr(
                          "yzImgUrl",
                          uploadResult.resultObject.resultObject.imageUrl
                        );
                        target.attr(
                          "weixinImgUrl",
                          uploadResult.resultObject.resultObject
                            .thirdPartyImageUrl
                        );

                        //显示图片
                        target.find("img").attr("src", filePath);
                        displayPicture(target, "show");

                        listen4Next();
                      } else {
                        var msg = "";
                        if (uploadResult.success.toString() == "false") {
                          msg = uploadResult.msg;
                        } else if (
                          uploadResult.resultObject.success.toString() ==
                          "false"
                        ) {
                          msg = uploadResult.resultObject.msg;
                        }
                        DialogUtil.dialogWarn(msg);
                      }
                    }
                  );
                } else {
                  DialogUtil.dialogWarn(selectResult.msg);
                }
              }
            );
          }
        }
      ],
      [
        {
          text: "取消",
          color: "danger",
          onClick: function() {}
        }
      ]
    ]);
  }

  //图片显示
  function displayPicture(target, showOrHide) {
    if (showOrHide == "show") {
      target.find("div.upload-before").hide();
      target.find("div.upload-after").show();
    } else if (showOrHide == "hide") {
      target.find("div.upload-before").show();
      target.find("div.upload-after").hide();
    }
  }

  //下一步：监听输入
  function listen4Next() {
    var shopName = $("#weixinOpenStep1Page4Wangshang input[name='shopName']")
      .val()
      .trim(); //门店名称
    var shortShopName = $(
      "#weixinOpenStep1Page4Wangshang input[name='shortShopName']"
    )
      .val()
      .trim(); //简称
    var merchantType = $(
      "#weixinOpenStep1Page4Wangshang div[name='merchantType'] input[type='hidden']"
    ).val(); //商户类型
    var businessRange = $(
      "#weixinOpenStep1Page4Wangshang div[name='businessRange'] input[type='hidden']"
    ).val(); //经营类目
    var hotline = $("#weixinOpenStep1Page4Wangshang input[name='hotline']")
      .val()
      .trim(); //客服电话
    var principalName = $(
      "#weixinOpenStep1Page4Wangshang input[name='principalName']"
    )
      .val()
      .trim(); //负责人
    var principalMobile = $(
      "#weixinOpenStep1Page4Wangshang input[name='principalMobile']"
    )
      .val()
      .trim(); //手机号
    var principalCard = $(
      "#weixinOpenStep1Page4Wangshang input[name='principalCard']"
    )
      .val()
      .trim(); //身份证号码
    var yzImgUrl4Front = $(
      "#weixinOpenStep1Page4Wangshang li[name='frontPicture']"
    ).attr("yzImgUrl"); //身份证正面：云纵
    var yzImgUrl4Back = $(
      "#weixinOpenStep1Page4Wangshang li[name='backPicture']"
    ).attr("yzImgUrl"); //身份证反面：云纵

    if (
      shopName &&
      shortShopName &&
      merchantType &&
      businessRange &&
      hotline &&
      principalName &&
      principalMobile &&
      principalCard &&
      yzImgUrl4Front &&
      yzImgUrl4Back
    ) {
      if (
        merchantType == Constants.weixin_wangshang.MERCHANT_TYPE.nature_person
      ) {
        //自然人
        $("#weixinOpenStep1Page4Wangshang .header li.right").removeClass(
          "disabled"
        );
      } else if (
        merchantType == Constants.weixin_wangshang.MERCHANT_TYPE.person_business
      ) {
        //个体工商户
        var registerNumber = $(
          "#weixinOpenStep1Page4Wangshang input[name='registerNumber']"
        )
          .val()
          .trim(); //工商注册号
        var yzImgUrl4BusinessLicense = $(
          "#weixinOpenStep1Page4Wangshang li[name='businessLicense']"
        ).attr("yzImgUrl"); //营业执照：云纵
        if (registerNumber && yzImgUrl4BusinessLicense) {
          $("#weixinOpenStep1Page4Wangshang .header li.right").removeClass(
            "disabled"
          );
        } else {
          $("#weixinOpenStep1Page4Wangshang .header li.right").addClass(
            "disabled"
          );
        }
      } else if (
        merchantType ==
        Constants.weixin_wangshang.MERCHANT_TYPE.enterprise_business
      ) {
        //企业商户
        // var bossName = $("#weixinOpenStep1Page4Wangshang input[name='bossName']").val();	//企业法人
        var registerNumber = $(
          "#weixinOpenStep1Page4Wangshang input[name='registerNumber']"
        )
          .val()
          .trim(); //工商注册号
        var yzImgUrl4BusinessLicense = $(
          "#weixinOpenStep1Page4Wangshang li[name='businessLicense']"
        ).attr("yzImgUrl"); //营业执照：云纵
        var yzImgUrl4AccountLicense = $(
          "#weixinOpenStep1Page4Wangshang li[name='accountLicense']"
        ).attr("yzImgUrl"); //开户人许可证：云纵
        if (
          registerNumber &&
          yzImgUrl4BusinessLicense &&
          yzImgUrl4AccountLicense
        ) {
          $("#weixinOpenStep1Page4Wangshang .header li.right").removeClass(
            "disabled"
          );
        } else {
          $("#weixinOpenStep1Page4Wangshang .header li.right").addClass(
            "disabled"
          );
        }
      }
    }else{
    $("#weixinOpenStep1Page4Wangshang .header li.right").addClass("disabled");
    }
  }

  /**
   * 测试接口
   * @type {Object}
   */
  var weixinOpenStep1Test = {
    testSendVerifyCode: function(options) {
      //发送验证码
      options.success({
        success: true
      });
    }
  };
})(document, window);
